WOLF core
WOLF: A Modular Estimation Framework for Robotics
XXXXXXXXXXXXXXXXXXXXXXXXvoid(0)" frameborder="0" name="MSearchResults" id="MSearchResults">
profiling_unit.h
Go to the documentation of this file.
1// WOLF - Copyright (C) 2020-2026
2// Institut de Robòtica i Informàtica Industrial, CSIC-UPC.
3// Authors: Joan Solà Ortega (jsola@iri.upc.edu) and
4// Joan Vallvé Navarro (jvallve@iri.upc.edu)
5// All rights reserved.
6//
7// This file is part of WOLF: http://www.iri.upc.edu/wolf
8// WOLF is free software: you can redistribute it and/or modify
9// it under the terms of the GNU General Public License version 3
10// as published by the Free Software Foundation.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with this program. If not, see <http://www.gnu.org/licenses/>.
19#pragma once
20
21#include <iostream>
22#include <iomanip>
23#include <chrono>
24#include <cmath>
25
26namespace wolf
27{
28// class ProfilingUnit
30{
31 protected:
32 bool running_;
34 unsigned int n_callbacks_;
36 std::chrono::microseconds acc_duration_us_;
37 std::chrono::microseconds max_duration_us_;
39 std::chrono::time_point<std::chrono::high_resolution_clock> first_clock_;
40 std::chrono::time_point<std::chrono::high_resolution_clock> start_clock_;
41
42 public:
47 ProfilingUnit(double desired_period_sec = -1);
48
52 void startProfiling();
53
57 void stopProfiling();
58
64 void printProfiling(const std::string& _tabs, std::ostream& stream = std::cout) const;
65
72 std::string printDuration(double _duration_microsec, int precision = 2) const;
73
78 void setDesiredPeriod(double _desired_period_sec);
79
84 bool isRunning() const;
85
90 unsigned int getNCallbacks() const;
91
96 double getDesiredPeriod() const;
97
102 double getAveragePeriod() const;
103
108 double getAccDuration() const;
109
114 double getAvgDuration() const;
115
120 double getMaxDuration() const;
121
126 double getVarDuration() const;
127
132 double getStDevDuration() const;
133};
134
135inline bool ProfilingUnit::isRunning() const
136{
137 return running_;
138}
139
140inline unsigned int ProfilingUnit::getNCallbacks() const
141{
142 return n_callbacks_;
143}
144
146{
147 return 1e-6 * desired_period_us_;
148}
149
150inline void ProfilingUnit::setDesiredPeriod(double _desired_period_sec)
151{
152 desired_period_us_ = 1e6 * _desired_period_sec;
153}
154
156{
157 return 1e-6 * average_period_us_;
158}
159
160inline double ProfilingUnit::getAccDuration() const
161{
162 return 1e-6 * acc_duration_us_.count();
163}
164
165inline double ProfilingUnit::getAvgDuration() const
166{
167 return (n_callbacks_ > 0 ? 1e-6 * acc_duration_us_.count() / n_callbacks_ : 0.0);
168}
169
170inline double ProfilingUnit::getMaxDuration() const
171{
172 return 1e-6 * max_duration_us_.count();
173}
174
175inline double ProfilingUnit::getVarDuration() const
176{
177 return 1e-12 * var_duration_us_;
178}
179
181{
182 return 1e-6 * sqrt(var_duration_us_);
183}
184
185} // namespace wolf
Definition: profiling_unit.h:30
std::chrono::time_point< std::chrono::high_resolution_clock > start_clock_
Time point of the start clock.
Definition: profiling_unit.h:40
double desired_period_us_
Desired period in microseconds (< 0 for not specified)
Definition: profiling_unit.h:33
double getAvgDuration() const
Get the average duration.
Definition: profiling_unit.h:165
double getMaxDuration() const
Get the maximum duration.
Definition: profiling_unit.h:170
double getStDevDuration() const
Get the standard deviation of the duration.
Definition: profiling_unit.h:180
unsigned int getNCallbacks() const
Get the number of callbacks.
Definition: profiling_unit.h:140
unsigned int n_callbacks_
Number of callbacks.
Definition: profiling_unit.h:34
double getVarDuration() const
Get the variance of the duration.
Definition: profiling_unit.h:175
void setDesiredPeriod(double _desired_period_sec)
Set the desired period.
Definition: profiling_unit.h:150
double getAccDuration() const
Get the accumulated duration.
Definition: profiling_unit.h:160
std::chrono::microseconds max_duration_us_
Maximum duration in microseconds.
Definition: profiling_unit.h:37
ProfilingUnit(double desired_period_sec=-1)
Constructor.
Definition: profiling_unit.cpp:24
double var_duration_us_
Variance of the duration in microseconds squared.
Definition: profiling_unit.h:38
void startProfiling()
Start profiling.
Definition: profiling_unit.cpp:35
void printProfiling(const std::string &_tabs, std::ostream &stream=std::cout) const
Print profiling results.
Definition: profiling_unit.cpp:107
std::chrono::time_point< std::chrono::high_resolution_clock > first_clock_
Time point of the first clock.
Definition: profiling_unit.h:39
double getAveragePeriod() const
Get the average period.
Definition: profiling_unit.h:155
std::chrono::microseconds acc_duration_us_
Accumulated duration in microseconds.
Definition: profiling_unit.h:36
std::string printDuration(double _duration_microsec, int precision=2) const
Print provided duration (in us) in the most appropiate units with specified precision.
Definition: profiling_unit.cpp:80
void stopProfiling()
Stop profiling.
Definition: profiling_unit.cpp:44
double getDesiredPeriod() const
Get the desired period.
Definition: profiling_unit.h:145
bool running_
True if profiling is running.
Definition: profiling_unit.h:32
bool isRunning() const
Check if profiling is running.
Definition: profiling_unit.h:135
double average_period_us_
Average period in microseconds.
Definition: profiling_unit.h:35
Jet< T, N > sqrt(const Jet< T, N > &f)
Definition: jet.h:617
Definition: capture_base.h:23